package views{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TextEvent;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.text.StyleSheet;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	import mx.controls.listClasses.*;
	import mx.core.DPIClassification;
	import mx.core.FlexGlobals;
	import mx.core.mx_internal;
	import mx.graphics.BitmapFillMode;
	import mx.graphics.BitmapScaleMode;
	import mx.styles.CSSStyleDeclaration;
	import mx.utils.DensityUtil;
	
	import spark.components.IconItemRenderer;
	import spark.components.supportClasses.StyleableTextField;
	import spark.core.ContentCache;
	import spark.core.DisplayObjectSharingMode;
	import spark.core.IContentLoader;
	import spark.core.IGraphicElement;
	import spark.core.IGraphicElementContainer;
	import spark.core.ISharedDisplayObject;
	import spark.primitives.BitmapImage;
	import spark.utils.MultiDPIBitmapSource;
	
	
	
	public class NewIconItemRenderer extends IconItemRenderer
		implements IGraphicElementContainer, ISharedDisplayObject {
		

		
		public function NewIconItemRenderer(){
			super();
			iconWidth = 48;
		}
		
		
		
		//----------------------------------
		//  news
		//----------------------------------
		
		private var _news:String = "";
		private var newsText:String = "";
		private var _newsField:String;
		protected var newsDisplay:StyleableTextField;
		private var newsFieldOrFunctionChanged:Boolean;
		private var newsChanged:Boolean;
		
		private var oldUnscaledWidth:Number;
		
		public function get newsField():String
		{
			return _newsField;
		}
		
		public function set newsField(value:String):void
		{
			if (value == _newsField)
				return;
			
			_newsField = value;
			newsFieldOrFunctionChanged = true;
			newsChanged = true;
			
			invalidateProperties();
		}
		
	
		private var _newsFunction:Function;
		
		public function get newsFunction():Function
		{
			return _newsFunction;
		}

		public function set newsFunction(value:Function):void
		{
			if (value == _newsFunction)
				return;
			
			_newsFunction = value;
			newsFieldOrFunctionChanged = true;
			newsChanged = true;
			
			invalidateProperties(); 
		}
		
		public function get news():String
		{
			return _news;
		}
		
		public function set news(value:String):void
		{
			if (value == _news)
				return;
			
			_news = value;
			
			// Push the label down into the labelTextField,
			// if it exists
			if (newsDisplay)
			{
				newsDisplay.text = _news;
				invalidateSize();
			}
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			if (!newsDisplay)
			{
				createNewsDisplay();
				newsDisplay.text = _news;
			}
		}
		
		
		protected function createNewsDisplay():void
		{
			newsDisplay = StyleableTextField(createInFontContext(StyleableTextField));
			newsDisplay.styleName = this;
			newsDisplay.editable = false;
			newsDisplay.selectable = false;
			newsDisplay.multiline = false;
			newsDisplay.wordWrap = false;
			
			newsDisplay.setStyle("fontSize", "12");
			
			addChild(newsDisplay);
		}
		/**
		protected function createMessageDisplay():void
		{
			messageDisplay = StyleableTextField(createInFontContext(StyleableTextField));
			messageDisplay.styleName = this;
			messageDisplay.editable = false;
			messageDisplay.selectable = false;
			messageDisplay.multiline = true;
			messageDisplay.wordWrap = true;
			
			var messageStyleName:String = getStyle("messageStyleName");
			if (messageStyleName)
			{
				var styleDecl:CSSStyleDeclaration =
					styleManager.getMergedStyleDeclaration("." + messageStyleName);
				
				if (styleDecl)
					messageDisplay.styleDeclaration = styleDecl;
			}
			
			addChild(messageDisplay);
		}*/
		
		protected function destroyNewsDisplay():void
		{
			removeChild(newsDisplay);
			newsDisplay = null;
		}
		/*
		override protected function measure():void
		{
			// don't call super.measure() because there's no need to do the work that's
			// in there--we do it all in here.
			super.measure();
			
			
			var hasNews:Boolean = newsDisplay && newsDisplay.text != "";
			var newsWidth:Number = 0;
			var newsHeight:Number = 0;
			
			
			
			if (hasNews)
			{
				oldUnscaledWidth = unscaledWidth;
				
				var myIconWidth:Number = 0;
				var myIconHeight:Number = 0;
				myIconWidth = (isNaN(iconWidth) ? getElementPreferredWidth(iconDisplay) : iconWidth);
				myIconHeight = (isNaN(iconHeight) ? getElementPreferredHeight(iconDisplay) : iconHeight);
				
				var decoratorWidth:Number = 0;
				var decoratorHeight:Number = 0;
				
				if (decoratorDisplay)
				{
					decoratorWidth = getElementPreferredWidth(decoratorDisplay);
				}
				
				var paddingAndGapWidth:Number = getStyle("paddingLeft") + getStyle("paddingRight");
				var newsDisplayEstimatedWidth:Number = oldUnscaledWidth - paddingAndGapWidth - myIconWidth - decoratorWidth;
				
				setElementSize(newsDisplay, newsDisplayEstimatedWidth, NaN);
				
				newsWidth = getElementPreferredWidth(newsDisplay);
				newsHeight = getElementPreferredHeight(newsDisplay);
			}
			measuredHeight = measuredHeight+newsHeight;
			
			measuredMinHeight = measuredHeight+newsHeight;
		}*/
		
		override protected function commitProperties():void
		{
			super.commitProperties();
			
			if (newsFieldOrFunctionChanged)
			{
				newsFieldOrFunctionChanged = false;
				
				// let's see if we need to create or remove it
				if ((newsField || (newsFunction != null)) && !newsDisplay)
				{
					createNewsDisplay();
				}
				else if (!(newsField || (newsFunction != null)) && newsDisplay)
				{
					destroyNewsDisplay();
				}
				
				invalidateSize();
				invalidateDisplayList();
			}
			
			if (newsChanged)
			{
				newsChanged = false;
				
				if (newsFunction != null)
				{
					newsText = newsFunction(data);
					newsDisplay.text = newsText;
				}
				else if (newsField)
				{
					try
					{
						if (newsField in data && data[newsField] != null)
						{
							newsText = data[newsField];
							newsDisplay.text = newsText;
						}
						else
						{
							newsText = "";
							newsDisplay.text = newsText;
						}
					}
					catch(e:Error)
					{
						newsText = "";
						newsDisplay.text = newsText;
					}
				}
			}
			
		}
		
		
		
		
		/*** Background ***/
		override protected function drawBackground(unscaledWidth:Number, unscaledHeight:Number):void{
			/////unscaledHeight=iconHeight;
			super.drawBackground(unscaledWidth, unscaledHeight);
			// separators are a highlight on the top and shadow on the bottom
			/*var topSeparatorColor:uint = 0x6F6F6F;
			var topSeparatorAlpha:Number = 1;
			var bottomSeparatorColor:uint = 0x313031;
			var bottomSeparatorAlpha:Number = 1;
			
			// draw separators
			// don't draw top separator for down and selected states
			if (!(selected || down))
			{
			graphics.beginFill(topSeparatorColor, topSeparatorAlpha);
			graphics.drawRect(0, 0, unscaledWidth, 2);
			graphics.endFill();
			}
			
			graphics.beginFill(bottomSeparatorColor, bottomSeparatorAlpha);
			graphics.drawRect(0, unscaledHeight - (mx_internal::isLastItem ? 0 : 2), unscaledWidth, 2);
			graphics.endFill();*/
		}
		
		
		override protected function layoutContents(unscaledWidth:Number,
												   unscaledHeight:Number):void
		{
			
			setStyle("paddingTop", 0);
			setStyle("paddingBottom", 0);
			setStyle("paddingLeft", 5);
			
			super.layoutContents(unscaledWidth, iconHeight);
			
			/*** Positionner l'icône ***/
			var paddingTop:Number    = getStyle("paddingTop");
			var paddingBottom:Number = getStyle("paddingBottom");
			var verticalAlign:String = getStyle("verticalAlign");
			
			var vAlign:Number;
			
			if (verticalAlign == "top")
				vAlign = 0;
			else if (verticalAlign == "bottom")
				vAlign = 1;
			else // if (verticalAlign == "middle")
				vAlign = 0.5;
			
			var viewHeight:Number = unscaledHeight - paddingTop  - paddingBottom;
			var iconDisplayY:Number = Math.round(vAlign * (viewHeight - iconHeight)) + paddingTop;
			
			setElementPosition(iconDisplay, 0, iconDisplayY);
			
			
			
			/*** Positionner le label ***/
			var verticalGap:Number = getStyle("verticalGap");
			var horizontalGap:Number = getStyle("horizontalGap");
			var paddingLeft:Number   = getStyle("paddingLeft");
			
			var totalHeight:Number = 0;
			var labelComponentsY:Number = 0; 
			
			// Heights used in our alignment calculations.  We only care about the "real" ascent 
			var labelAlignmentHeight:Number = 0; 
			var messageAlignmentHeight:Number = 0; 
			
			horizontalGap=0;
			
			var labelComponentsX:Number = paddingLeft;
			if (iconDisplay)
				labelComponentsX += iconWidth;// + horizontalGap;
			
			labelAlignmentHeight = getElementPreferredHeight(labelDisplay);
			
			totalHeight = labelAlignmentHeight + messageAlignmentHeight + verticalGap;          
			labelComponentsY = Math.round(vAlign * (viewHeight - totalHeight)) + paddingTop;
			
			if (labelDisplay)
				setElementPosition(labelDisplay, labelComponentsX, 2*iconHeight/16);

			if (messageDisplay)
			{
			var messageY:Number = labelComponentsY + labelAlignmentHeight + verticalGap;
			setElementPosition(messageDisplay, labelComponentsX, 6*iconHeight/16);
			}
			
			
			/** Gestion des news **/
			var hasNews:Boolean = newsDisplay && newsDisplay.text != "";
			
			if (hasNews)
				setElementPosition(newsDisplay, labelComponentsX, 10*iconHeight/16);
		}
		
		public static var styleSheet:StyleSheet = null;
		
		override protected function createMessageDisplay():void{
			messageDisplay = StyleableTextField(createInFontContext(StyleableTextField));
			messageDisplay.styleName = this;
			messageDisplay.editable = false;
			messageDisplay.selectable = false;
			messageDisplay.multiline = true;
			messageDisplay.wordWrap = true;
			messageDisplay.setStyle("fontSize", "9");
			addChild(messageDisplay);
		}
		
		override protected function createLabelDisplay():void
		{
			labelDisplay = StyleableTextField(createInFontContext(StyleableTextField));
			labelDisplay.styleName = this;
			labelDisplay.editable = true;
			labelDisplay.selectable = false;
			labelDisplay.multiline = false;
			labelDisplay.wordWrap = false;
			
			labelDisplay.setStyle("fontWeight", "bold");
			
			addChild(labelDisplay);
		}
		
		
	}
	
	
	
}